use utf8;

############################################################################################
#                             Définition de l'onglet Adhérents                             #
############################################################################################

my $subscriber = $blocnote->add('Adherent', -label => 'Adhérents',);
$subscriber->configure( -background => 'white',);

#Message de retour
my $messageSubscriber = '';

#Mise en place du titre
my $c = $subscriber->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1); 
$subscriber->Label ( -text => "Rechercher / Ajouter un Adhérent", -background => 'white')->pack();
$c = $subscriber->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1); 

#Définition des champs de recherche
my $fieldSubscriber = $subscriber->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

$fieldSubscriber->Label( -text => 'Nom:', -background => 'white', -justify => 'left')->grid(-row => 0, -column => 0, -sticky => 'w');
my $name = $fieldSubscriber->Entry()->grid(-row => 0, -column => 1, -sticky => 'w');

$fieldSubscriber->Label(-text => 'Prénom:', -background => 'white')->grid(-row => 1, -column => 0, -sticky => 'w');
my $firstName = $fieldSubscriber->Entry()->grid(-row => 1, -column => 1, -sticky => 'w');

$fieldSubscriber->Label(-text => 'Login:', -background => 'white')->grid(-row => 2, -column => 0, -sticky => 'w');
my $login = $fieldSubscriber->Entry()->grid(-row => 2, -column => 1, -sticky => 'w');

$fieldSubscriber->Label( -text => 'Filière:', -background => 'white',)->grid(-row => 3, -column => 0, -sticky => 'w',);
my $filiere = '';
$fieldSubscriber->BrowseEntry(-listheight => 4, -choices => ['Informatique', 'Télécommunication', 'Électronique', 'Matmeca'], -variable => \$filiere)->grid(-row => 3, -column => 1, -sticky => 'w');

$fieldSubscriber->Label( -text => 'Promotion:', -background => 'white',)->grid(-row => 4, -column => 0, -sticky => 'w');
my $promotion = $fieldSubscriber->Entry()->grid(-row => 4, -column => 1, -sticky => 'w');

$fieldSubscriber->Label( -text => 'Date d\'adhésion:', -background => 'white',)->grid(-row => 5, -column => 0, -sticky => 'w');
my $dateSubscription = $fieldSubscriber->Entry()->grid(-row => 5, -column => 1, -sticky => 'w');

my $isInBureau = 2;
$fieldSubscriber->Label(-text => 'Membre du bureau:', -background => 'white')->grid(-row => 6, -column => 0, -sticky => 'w');
$fieldSubscriber->Radiobutton(-background => 'white', -activebackground => 'white', -highlightthickness => 0, -variable => \$isInBureau, -value=>1, -text => "Oui")->grid(-row => 6, -column => 1, -sticky => 'w');
$fieldSubscriber->Radiobutton(-background => 'white', -activebackground => 'white', -highlightthickness => 0, -variable => \$isInBureau, -value=>0, -text => "Non")->grid(-row => 6, -column => 1, -sticky => 'e',-ipadx=>20);


my $buttonFrame = $subscriber->Frame(-background => 'white')->pack(-anchor => 'n');

$buttonFrame->Button(-text => 'Ajouter',
                     -command => [sub{
        				    if($name->get ne '' and 
        				       $firstName->get ne '' and 
        				       $login->get ne '' and 
        				       $filiere ne '' and 
        				       $promotion->get ne '' and 
        				       $dateSubscription->get ne ''){
                        if($isInBureau != 2) {
                          AddSubscriber();
                        }
                        else{
                          $messageSubscriber = "Pour ajouter un adhérent merci de renseigner tous les champs.";
                    }}}])->grid(-row => 6, -column => 0, -pady => 5, -sticky => 'w', -padx => 7);
$buttonFrame->Button(-text => 'Rechercher', -command => [\&SearchSubscriber])->grid(-row => 6, -column => 1, -pady => 5, -sticky => 'w', -padx => 7);
$buttonFrame->Button(-text => 'Effacer', -command =>[\&ClearSubscriber,$name,$firstName,$login,$promotion,$dateSubscription, \$filiere, \$isInBureau])->grid(-row => 6, -column => 2, -pady => 5, -sticky => 'w', -padx => 7);

$subscriber->Label(-textvariable => \$messageSubscriber, -background => 'white')->pack();

############################################################################################
#                            Fonctions de gestion des Adhérents                            #
############################################################################################

#Fonction ajoutant un adhérent
sub AddSubscriber {
  my $dialog_box = $window->DialogBox(-title => 'Ajout d\'un membre', -buttons => ["OK"], -background => 'white');
  gestion_bd::addSubscriber($name->get,$firstName->get,$promotion->get,$filiere,$login->get,$dateSubscription->get);
  if ($isInBureau == 1) {
    gestion_bd::addBureauMember($name->get,$firstName->get,$promotion->get,$filiere,$login->get,$dateSubscription->get,$dateSubscription->get);
  }
  $dialog_box->Label(-text => "Adhérent ajouté !", -background => 'white')->pack();
  $dialog_box->Show();
}

#Fonction supprimant un adhérent
sub DeleteSubscriber {
  my $dialog_box = $window->DialogBox(-title  => 'Suppression d\'un membre', -buttons => ["OK"], -background => 'white');
  gestion_bd::deleteMember($_[0]);
  $dialog_box->Label(-text => "Adhérent supprimé !", -background => 'white')->pack();
  $dialog_box->Show();
}

#Fonction affichant le résultat de la recherche d'un adherent
sub SearchSubscriber {
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title => 'Résultat de la recherche', -buttons    => ["Modifier", "Notes", "Commentaires", "Emprunts", "Supprimer", "Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Nom", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Prénom", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Login", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");
  $dialog_box->Label(-text => "Filière", -relief =>'groove',)->grid(-row => 0, -column => 4, -sticky => "nsew");
  $dialog_box->Label(-text => "Promotion", -relief =>'groove',)->grid(-row => 0, -column => 5, -sticky => "nsew");
  $dialog_box->Label(-text => "Date d'adhésion", -relief =>'groove',)->grid(-row => 0, -column => 6, -sticky => "nsew");
  $dialog_box->Label(-text => "Membre du Bureau", -relief =>'groove',)->grid(-row => 0, -column => 7, -sticky => "nsew");
    
  my $membersList = gestion_bd::searchMember($name->get, $firstName->get, $promotion->get, $filiere, $login->get, $dateSubscription->get, $isInBureau);
  my $member;
  foreach my $subscribers (sort {$membersList->{$a}{nomPersonne} cmp $membersList->{$b}{nomPersonne}} keys %{$membersList}) {
    $dialog_box->Radiobutton(-value=>$membersList->{$subscribers}{login}, -variable=>\$member)->grid(-row => $i, -column => 0,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{nomPersonne}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{prenomPersonne}, -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{login}, -background => 'white')->grid(-row => $i, -column => 3,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{filiere}, -background => 'white')->grid(-row => $i, -column => 4,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{promotion}, -background => 'white')->grid(-row => $i, -column => 5,);
    $dialog_box->Label(-text => gestion_bd::convertDate($membersList->{$subscribers}{dateAdhesion}), -background => 'white')->grid(-row => $i, -column => 6,);

    my $testBureau = gestion_bd::isInBureau($membersList->{$subscribers}{idAdherent});
    if($testBureau){
      $dialog_box->Label(-text => "Oui", -background => 'white')->grid(-row => $i, -column => 7,);
    }
    else{
      $dialog_box->Label(-text => "Non", -background => 'white')->grid(-row => $i, -column => 7,);
    }
    $i = $i + 1;
  }

  my $reponse = $dialog_box->Show();

  if ($reponse eq "Modifier" and defined($member)) {
    ModifySubscriber($member);
  }
  elsif ($reponse eq "Emprunts" and defined($member)) {
    ListBorrowBook($membersList->{$member}{idAdherent});
  }
  elsif ($reponse eq "Notes" and defined($member)) {
    ListOfMark($membersList->{$member}{idAdherent});
  }
  elsif ($reponse eq "Commentaires" and defined($member)) {
    ListOfCommit($membersList->{$member}{idAdherent});
  }
  elsif ($reponse eq "Supprimer" and defined($member)) {
    DeleteSubscriber($member);
  }
  elsif (($reponse eq "Modifier" or $reponse eq "Supprimer" or $reponse eq "Emprunts")  and !defined($member)) {
    errorBox("Veuillez selectionner un adhérent !");
    SearchSubscriber();
  }
}

sub ListOfMark{
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title => 'Liste des notes', -buttons => ["Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Nom du Jeu", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Note", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Commentaire", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");

  my $gamesList = gestion_bd::listOfMark($_[0]);

  foreach my $g (sort {$gamesList->{$a}{titre} cmp $gamesList->{$b}{titre}} keys %{$gamesList}) {
    $dialog_box->Label(-text => $gamesList->{$g}{nomJeu}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => $gamesList->{$g}{note}, -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => $gamesList->{$g}{commentaire}, -background => 'white')->grid(-row => $i, -column => 3,);
    $i = $i + 1;
  }
  $dialog_box->Show();
}

sub ListOfCommit{
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title => 'Liste des notes', -buttons => ["Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Lieu", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Date", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Commentaire", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");

  my $eventsList = gestion_bd::listOfCommit($_[0]);

  foreach my $e (sort {$eventsList->{$a}{titre} cmp $eventsList->{$b}{titre}} keys %{$eventsList}) {
    $dialog_box->Label(-text => $eventsList->{$e}{lieu}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => $eventsList->{$e}{dateEvenement}, -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => $eventsList->{$e}{commentaire}, -background => 'white')->grid(-row => $i, -column => 3,);
    $i = $i + 1;
  }
  $dialog_box->Show();
}

sub ListBorrowBook{
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title      => 'Résultat de la recherche', -buttons    => ["Modifier", "Supprimer", "Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Titre", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Nom Auteur", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Prenom auteur", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");
  $dialog_box->Label(-text => "Editeur", -relief =>'groove',)->grid(-row => 0, -column => 4, -sticky => "nsew");
  $dialog_box->Label(-text => "Date d'emprunt", -relief =>'groove',)->grid(-row => 0, -column => 5, -sticky => "nsew");
  $dialog_box->Label(-text => "Date de retour", -relief =>'groove',)->grid(-row => 0, -column => 6, -sticky => "nsew");

  my $booksList = gestion_bd::listBorrowBook($_[0],"");
  my $book;

  foreach my $b (sort {$booksList->{$a}{titre} cmp $booksList->{$b}{titre}} keys %{$booksList}) {
    $dialog_box->Radiobutton(-value=>$booksList->{$b}{ISBN}, -variable=>\$book)->grid(-row => $i, -column => 0,);
    $dialog_box->Label(-text => $booksList->{$b}{titre}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => $booksList->{$b}{nomPersonne}, -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => $booksList->{$b}{prenomPersonne}, -background => 'white')->grid(-row => $i, -column => 3,);
    $dialog_box->Label(-text => $booksList->{$b}{nomEditeur}, -background => 'white')->grid(-row => $i, -column => 4,);
    $dialog_box->Label(-text => gestion_bd::convertDate($booksList->{$b}{dateEmprunt}), -background => 'white')->grid(-row => $i, -column => 5,);
    $dialog_box->Label(-text => gestion_bd::convertDate($booksList->{$b}{dateRetour}), -background => 'white')->grid(-row => $i, -column => 6,);
    $i = $i + 1;
  }

  my $reponse = $dialog_box->Show();

  if ($reponse eq "Modifier" and defined($book)) {
    ModifyBorrower($book,$_[0]);
  }
  elsif ($reponse eq "Supprimer" and defined($book)) {
    DeleteBorrower($book,$_[0]);
  }
  elsif ($reponse eq "Modifier" or $reponse eq "Supprimer" or $reponse eq "Emprunter") {
    errorBox("Veuillez selectionner un livre !");
    ListBorrowBook($_[0]);
  }
}

#Fonction modifiant un adhérant
sub ModifySubscriber {
  my $dialog_box = $window->DialogBox(-title => "Modification d'un adhérent", -buttons    => ["Enregistrer", "Annuler"], -background => 'white', -default_button => 'Annuler');
 
  my $membersList = gestion_bd::searchMember("", "", "", "", $_[0], "", 2);
  my $fieldDialog_Box = $dialog_box->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);
  
  $fieldDialog_Box->Label( -text => 'Nom:', -background => 'white', -justify => 'left')->grid(-row => 0, -column => 0, -sticky => 'w');
  my $name = $fieldDialog_Box->Entry(-textvariable => $membersList->{$_[0]}{nomPersonne})->grid(-row => 0, -column => 1, -sticky => 'w');
  
  $fieldDialog_Box->Label(-text => 'Prénom:', -background => 'white')->grid(-row => 1, -column => 0, -sticky => 'w');
  my $firstName = $fieldDialog_Box->Entry(-textvariable => $membersList->{$_[0]}{prenomPersonne})->grid(-row => 1, -column => 1, -sticky => 'w');
  
  $fieldDialog_Box->Label( -text => 'Filière:', -background => 'white',)->grid(-row => 3, -column => 0, -sticky => 'w',);
  my $filiere = "$membersList->{$_[0]}{filiere}";
  $fieldDialog_Box->BrowseEntry(-listheight => 4, -choices => ['Informatique', 'Télécommunication', 'Électronique', 'Matmeca'], -variable => \$filiere)->grid(-row => 3, -column => 1, -sticky => 'w');
  
  $fieldDialog_Box->Label( -text => 'Promotion:', -background => 'white',)->grid(-row => 4, -column => 0, -sticky => 'w');
  my $promotion = $fieldDialog_Box->Entry(-textvariable => $membersList->{$_[0]}{promotion})->grid(-row => 4, -column => 1, -sticky => 'w');
  
  $fieldDialog_Box->Label( -text => 'Date d\'adhérence:', -background => 'white',)->grid(-row => 5, -column => 0, -sticky => 'w');
  my $dateSubscription = $fieldDialog_Box->Entry(-textvariable => gestion_bd::convertDate($membersList->{$_[0]}{dateAdhesion}))->grid(-row => 5, -column => 1, -sticky => 'w');
  
  my $reponse = $dialog_box->Show();

  if ($reponse eq "Enregistrer") {
    gestion_bd::updateMember($name->get,$firstName->get,$promotion->get,$filiere,$membersList->{$_[0]}{login},$dateSubscription->get);
    dialogBox("Information","Modification enregistrée !");
  }
}
